package com.yx.shop.utils;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

/**
 * @Classname DbUtils
 * @Description TODO:
 * @Date 2021/04/22 18:58
 * @Created by yx
 */
public class JDBCUtils {
    private static DruidDataSource dataSource = null;
    private static final ThreadLocal<Connection> THREAD_LOCAL = new ThreadLocal<>();

    /**
     * @Author:yx
     * @return
     * @Date 2021/04/22 19:20
     * @param null
     * @Description:初始化
     **/
    static {
        Properties properties = new Properties();
        try {
            properties.load(JDBCUtils.class.getClassLoader().getResourceAsStream("jdbc.properties"));
            dataSource = (DruidDataSource) DruidDataSourceFactory.createDataSource(properties);
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * @param
     * @return java.sql.Connection
     * @Author:yx
     * @Date 2021/04/22 19:19
     * @Description:获取连接对象
     **/
    public static Connection getConnection() {
        Connection connection = THREAD_LOCAL.get();
        if (connection == null) {
            try {
                connection = dataSource.getConnection();
                THREAD_LOCAL.set(connection);
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return connection;
    }

    /**
     * @param
     * @return void
     * @Author:yx
     * @Date 2021/04/22 19:19
     * @Description:开启事务
     **/
    public static void begin() {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param
     * @return void
     * @Author:yx
     * @Date 2021/04/22 19:19
     * @Description:提交事务
     **/
    public static void commit() {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param
     * @return void
     * @Author:yx
     * @Date 2021/04/22 19:19
     * @Description:回滚事务
     **/
    public static void rollback() {
        Connection connection = null;
        try {
            connection = getConnection();
            connection.rollback();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param connection
     * @param statement
     * @param resultSet
     * @return void
     * @Author:yx
     * @Date 2021/04/22 19:19
     * @Description:关闭资源
     **/
    public static void closeAll(Connection connection, Statement statement, ResultSet resultSet) {
        try {
            if (resultSet != null) {
                resultSet.close();
            }
            if (statement != null) {
                statement.close();
            }
            if (connection != null) {
                connection.close();
                THREAD_LOCAL.remove();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    /**
     * @param
     * @return com.alibaba.druid.pool.DruidDataSource
     * @Author:yx
     * @Date 2021/04/22 19:18
     * @Description:获取连接池
     **/
    public static DruidDataSource getDataSource() {
        return dataSource;
    }

}
